#include <stdio.h>
#include <string.h>
#include <gmssl/sm3.h>

#define BUFFER_SIZE 1024  // 定义缓冲区大小

int main() {
    FILE *file;
    char buffer[BUFFER_SIZE];
    size_t bytes_read;
    unsigned char hash[SM3_DIGEST_SIZE];  // SM3_DIGEST_SIZE is the length of the hash

    SM3_CTX ctx;
    sm3_init(&ctx);  // Initialize the hash context

    // 打开文件
    file = fopen("sn.txt", "rb");
    if (file == NULL) {
        perror("Error opening file");
        return 1;
    }

    // 读取文件内容并更新哈希
    while ((bytes_read = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
        sm3_update(&ctx, (const unsigned char *)buffer, bytes_read);
    }

    // 关闭文件
    fclose(file);

    // 完成哈希计算
    sm3_finish(&ctx, hash);

    // 输出哈希值
    printf("SM3 hash: ");
    for (int i = 0; i < SM3_DIGEST_SIZE; i++) {
        printf("%02x", hash[i]);
    }
    printf("\n");

    return 0;
}

